package com.test.qnews.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.qnews.R;
import com.test.qnews.beans.StarBeans;

import java.util.List;

/**
 * Created by dllo on 16/1/18.
 */
public class StarAdapter extends BaseAdapter {
    private List<StarBeans> data;
    private Context context;

    public StarAdapter(List<StarBeans> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Viewholder holder = new Viewholder();
        if(convertView == null){  //判断行布局为空
            //加载行布局文件
            convertView = LayoutInflater.from(context).inflate(R.layout.item_star,null);
            //绑定行布局ID
            holder.iv = (ImageView) convertView.findViewById(R.id.item_star_iv);
            holder.name_tv = (TextView) convertView.findViewById(R.id.item_star_tv);
            //将缓存类对象设置到行布局当中
            convertView.setTag(holder);
        }else{
            //直接使用行布局
            holder = (Viewholder) convertView.getTag();
        }

            holder.iv.setImageResource(data.get(position).getImage());
            holder.name_tv.setText(data.get(position).getName());

        return convertView;
    }

    //刷新每个按钮的数据
    public void refreshData(List<StarBeans> data){
        this.data = data;
        notifyDataSetChanged();
    }

    //创建缓存类
    private class Viewholder{
        private ImageView iv;
        private TextView name_tv;
    }

}

